#include "mymap.h"
#include <QUrl>

MyMap::MyMap(QObject *parent) :
    QGraphicsScene(parent)
{
  startline=NULL;
  map = NULL;
  endline=NULL;
  line=NULL;
  newline = true;
  selectedTool=0;
  scale=0;
  lineL=0;
  this->clear();
  pen = new QPen(Qt::red, 2, Qt::SolidLine);
}

//устанавливаем картинку
void MyMap::setMap( QPixmap filename ){
   if ( map==NULL ) {
       map = this->addPixmap( filename );
   }
   this->map->setPixmap( filename );

   //refresh();
}

void MyMap::clear_map(){
    map=NULL;
    //map->setPixmap(QPixmap());
}

void MyMap::dropEvent(QGraphicsSceneDragDropEvent *event){


}

void MyMap::dragEnterEvent(QGraphicsSceneDragDropEvent *event){

    event->accept();
}

void MyMap::dragMoveEvent(QGraphicsSceneDragDropEvent *event){

    event->accept();
}

void MyMap::mouseMoveEvent(QGraphicsSceneMouseEvent *event){
event->accept();
}

void MyMap::mousePressEvent(QGraphicsSceneMouseEvent *event){
event->accept();
}

void MyMap::mouseReleaseEvent(QGraphicsSceneMouseEvent *event){
event->accept();
}

void MyMap::startLine( QPoint *pos ){
  if ( newline ){
      endline=NULL;
      startline=NULL;
      lineL=0;
      newline=false;
  }
  if ( line==NULL ) {
      line = new QGraphicsLineItem();
      this->addItem(line);
  }
  if ( startline==NULL ){
      startline = new QPoint(*pos);
      endline = new QPoint(*pos);
      if ( selectedTool==1 )   pen->setColor( Qt::red );
      if ( selectedTool==2 )   pen->setColor( Qt::blue );
      line->setPen(*pen);
      line->setLine( QLine(*startline,*startline) );
  }else{
      endline->setX( pos->x() );
      endline->setY( pos->y() );
      line->setLine( QLine(*startline,*endline) );
      if (  selectedTool==1 ){
          lineL = qSqrt(qPow(endline->x()-startline->x(),2)+qPow(endline->y()-startline->y(),2));
          emit lineScale( int(lineL) );
          newline=true;
      }else if ( selectedTool==2 ){
          lineL = qSqrt(qPow(endline->x()-startline->x(),2)+qPow(endline->y()-startline->y(),2));
          emit lineMeasure( lineL/scale );
          newline=true;
      }
  }
}

void MyMap::refresh(){
    if ( line!=NULL&&startline!=NULL&&endline!=NULL ){
        line->setLine( QLine(*startline,*endline) );
    }
}

void MyMap::setScale(float km){
    if ( lineL!=0 ){
        scale = lineL/km;
    }
}

void MyMap::setScale(float px,float km){
    scale = px/km;
}

float MyMap::getScale(){
    return scale;
}

void MyMap::setLine(QLineF *l){
    line->setLine(*l);
}

bool MyMap::isNullLine(){
    if ( line==NULL )
        return true;
    else return false;
}

void MyMap::cancelAll(){
    if ( startline!=NULL ) startline=NULL;
    if ( endline!=NULL ) endline=NULL;
    if ( !isNullLine() ) line->setLine( QLineF() );

    return;
}

int MyMap::getTool(){
    return selectedTool;
}

void MyMap::setTool(int t){
    selectedTool=t;
}

void MyMap::lineMeasure_emit(){

    emit lineMeasure( lineL/scale );
}
